﻿using ACC.Data.Entities.Common;
using ACC.Extensions.ClientBusiness;
using ACC.Extensions.Entities;
using ACC.Extensions.PCClient.Helper;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

namespace ACC.Extensions.PCClient.View
{
    /// <summary>
    /// Interaction logic for CastingResultWnd.xaml
    /// </summary>
    public partial class ResultWithReasonWnd : MetroWindow
    {
        public ResultWithReasonWnd(string strBarcode,bool bTest, List<DefectData> lstDefectData )
        {
            InitializeComponent();
            this.Owner = Application.Current.MainWindow;
            this.txtBarcode.Text = strBarcode;
            this.dbgReason.ItemsSource = lstDefectData;
            _lstDefectData = lstDefectData;
            _strBarcode = strBarcode;
            BitOK = 0;
            this.chxTestPiece.IsChecked = bTest;
            this.btnOK.Content = string.Format(LanguageHelper.LoadStringFromResource("BTN_RESULT_OK"), System.Environment.NewLine);
            this.btnNOK.Content = string.Format(LanguageHelper.LoadStringFromResource("BTN_RESULT_NOK"), System.Environment.NewLine);
            this.btnHold.Content = string.Format(LanguageHelper.LoadStringFromResource("BTN_RESULT_HOLDE"), System.Environment.NewLine);
            this.btnStartUp.Content = string.Format(LanguageHelper.LoadStringFromResource("BTN_RESULT_STARTUP"), System.Environment.NewLine);
        }

        public int BitOK{get;set;}

        public bool IsCancel;
        private bool _bIsTest;
        private string _strBarcode;
        private List<DefectData> _lstDefectData;
        public string Barcode
        {
            get
            {
                return _strBarcode;
            }
            set
            {
                value = _strBarcode;
            }
        }

        private UnloadVariable _unloadData = new UnloadVariable();
        public UnloadVariable UnloadData
        {
            get
            {
                return _unloadData;
            }
            set
            {
                _unloadData = value;
            }
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //dbgReason.Visibility = Visibility.Hidden;
            DialogResult = null;
            IsCancel = false;
           // this.Owner = Application.Current.MainWindow;
        }

        //全否
        private void CancelDataGridItems()
        {
            foreach (DefectData item in this.dbgReason.Items)
           {
               item.IsChecked = false;
           }
        }
        private void dbgReason_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //CancelDataGridItems();
            DefectData s = this.dbgReason.SelectedItem as DefectData;
            if (s.IsChecked == true)
            {
                s.IsChecked = false;
                this.dbgReason.SelectedItem = s;
            }
            else
                s.IsChecked = true;
            this.dbgReason.SelectedItem = s;
            this.dbgReason.Items.Refresh();
        }

        private UnloadVariable GetUnloadData(bool bIsOK)
        {
            List<ClientFailureBit> lstStatusBit = new List<ClientFailureBit>();
            if (bIsOK)
            {
                ClientFailureBit statusBit = new ClientFailureBit();
                statusBit.Bit = 1; //OK为1，NOK为0
                lstStatusBit.Add(statusBit);
                return new UnloadVariable() { IsTestUnit = _bIsTest, LstStatusBit = lstStatusBit };
            }
            else
            {
                foreach (DefectData item in this.dbgReason.Items)
                {
                    if (item.IsChecked == true)
                    {
                        ClientFailureBit statusBit = new ClientFailureBit();
                        statusBit.Bit = item.InBit;
                        statusBit.Description = item.DefectCode + ":" + item.DefectDescription;
                        statusBit.Name = item.StatusName;
                        lstStatusBit.Add(statusBit);
                    }
                }
                return new UnloadVariable() { IsTestUnit = _bIsTest, LstStatusBit = lstStatusBit };
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            BitOK = 1;
            if (dbgReason.Visibility == Visibility.Hidden)
            {
                _unloadData = GetUnloadData(true); //OK true ;NOK false
                DialogResult = true;
                this.Close();
            }
            else
            {
                dbgReason.Visibility = Visibility.Hidden;
            }
        }

        private void btnNOK_Click(object sender, RoutedEventArgs e)
        {
            BitOK = 0;
            //if (dbgReason.Visibility == Visibility.Hidden)
            //{
            //    dbgReason.Visibility = Visibility.Visible;
            //}
            //else
            //{
                _unloadData = GetUnloadData(false); //OK true ;NOK false
                if (UnloadData.LstStatusBit.Count == 0 && dbgReason.Items.Count > 0)
                {
                    MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), "请选择缺陷原因");
                    IsCancel = true;
                    return;
                }
                DialogResult = true;
                this.Close();
            //}
        }

        //Hold
        private void btnHold_Click(object sender, RoutedEventArgs e)
        {
            BitOK = 0;
            if (dbgReason.Visibility == Visibility.Hidden)
            {
                dbgReason.Visibility = Visibility.Visible;
            }
            else
            {
                _unloadData = GetUnloadData(false); //OK true ;NOK false
                _unloadData.IsHold = true;
                if (UnloadData.LstStatusBit.Count == 0 && dbgReason.Items.Count > 0)
                {
                    MessageDialogWnd.ShowDialog(LanguageHelper.LoadStringFromResource("MSG_WARNING"), "请选择缺陷原因");
                    IsCancel = true;
                    return;
                }
                DialogResult = true;
                this.Close();
            }
        }
        //Startup
        private void btnStartUp_Click(object sender, RoutedEventArgs e)
        {
            BitOK = 0;
            _unloadData = new UnloadVariable() { IsTestUnit = _bIsTest, IsStartUp = true };
            if (dbgReason.Visibility == Visibility.Visible)
            {
                dbgReason.Visibility = Visibility.Hidden;
            }
            else
            {
                DialogResult = true;
                this.Close();
            }
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            IsCancel = true;
            DialogResult = false;
            this.Close();
        }

        private void chxTestPiece_Click(object sender, RoutedEventArgs e)
        {

            UserValidateWnd wnd = new UserValidateWnd();
            wnd.Owner = this;
            var dialogResult = wnd.ShowDialog();
            if (dialogResult.HasValue && dialogResult.Value)
            {
                _bIsTest = this.chxTestPiece.IsChecked.Value;
                e.Handled = true;
            }
            else
            {
                this.chxTestPiece.IsChecked = _bIsTest;
                e.Handled = false;
            }
           
        }

        private void MetroWindow_Activated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

        private void MetroWindow_Deactivated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

    }

    //转换器
    public class BooleanToCustomConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool used = (bool)value;
            return used ? "Hidden" : "Visible";
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }



   
}
